/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

public class CommonUtil {

    /**
     * memecah string menjadi array berdasarkan delimiter tertentu
     * @param str
     * @param delimiter
     * @return
     */
    public final static String[] splitStr(String str, String delimiter) {
        if (str == null) {
            return null;
        }
        Vector vc = new Vector();
        String hasil[] = null;
        int pos = 0;
        int pos2 = 0;
        String hsl = "";
        int byk = str.length();
        int delimLen = delimiter.length();
        while (pos < byk) {
            pos2 = str.indexOf(delimiter, pos);
            if (pos2 == -1) {
                pos2 = byk;
            }
            hsl = str.substring(pos, pos2);
            vc.addElement(hsl);

            pos = pos2 + delimLen;
            if (pos == byk) {
                vc.addElement("");
            }
        }
        byk = vc.size();
        if (byk > 0) {
            hasil = new String[byk];
            for (int i = 0; i < byk; i++) {
                hasil[i] = (String) vc.elementAt(i);
            }
        } else {
            hasil = new String[]{""};
        }
        vc.removeAllElements();
        vc = null;
        return hasil;
    }

    /**
     * mengubah objek inputstream menjadi string
     * @param is
     * @return
     */
    public static String inputStreamToString(InputStream is) {
        String result = "";
        try {
            int ch = 0;
            byte[] buf = new byte[1024];
            StringBuffer sb = new StringBuffer();
            do {
                try {
                    ch = is.read(buf);
                } catch (Exception e) {
                    if (sb.length() == 0) {
                        System.gc();
                        throw e;
                    } else {
                        ch = -1;
                    }
                }
                if (ch != -1) {
                    System.gc();
                    sb.append(new String(buf, 0, ch, "ISO-8859-1"));
                }
            } while (ch != -1);
            result = sb.toString();
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * mengubah objek string menjadi inputstream
     * @param str
     * @return
     */
    public static InputStream stringToInputStream(String str) {
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return is;
    }

    /**
     * validasi input yg diperbolehkan hanya huruf a-z 0-9 koma(;) dan spasi( )
     * @param input
     * @return
     */
    public static boolean validateInput(String input){
        boolean valid = false;
        char c;
        for (int i = 0; i < input.length(); i++) {
            c=input.charAt(i);
            if((c>=48 && c<=57)||(c>=97&&c<=122) || c==59 || c==32){
                valid = true;
            }else{
                valid = false;
                break;
            }
        }
        return valid;
    }
}
